<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/2/27
  Time: 19:57
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>轮播图管理模块</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css">
    <script src="${pageContext.request.contextPath}/layui/layui.js" type="text/javascript"></script>
</head>
<body>
    <!--添加表单-->
    <form class="layui-form" id="addForm" lay-filter="addFormFilter" style="display: none;padding: 15px 15px 5px 5px"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
        <div class="layui-form-item">
            <label class="layui-form-label">轮播图名称</label>
            <div class="layui-input-block">
                <input type="hidden" name="bannerImageUrl"/>
                <input type="text" name="bannerOldName" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">轮播图片</label>
            <div class="layui-input-block">
                <button type="button" class="layui-btn" id="addImg">
                    <i class="layui-icon">&#xe67c;</i>上传图片
                </button>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">轮播图状态</label>
            <div class="layui-input-block">
                <<input type="radio" name="bannerState" value="0" title="已过期">
                <input type="radio" name="bannerState" value="1" title="禁用">
                <input type="radio" name="bannerState" value="2" title="启用">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">上传时间</label>
            <div class="layui-inline">
                <input type="text" name="bannerDate" class="layui-input" id="addDate">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">轮播图描述</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入内容" name="bannerDescription" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="addFilter">添加</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
    <!--添加按钮-->
    <button class="layui-btn" id="add">添加</button>
    <!--展示表格-->
    <table id="myTable" lay-filter="tableFilter"></table>
    <!--表格显示图片-->
    <script type="text/html" id="showImg">
        <img src="${pageContext.request.contextPath}/banner/{{d.bannerImageUrl}}" height="50px" width="100px"/>
    </script>
    <!--toolbar组件-->
    <script type="text/html" id="bar">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-danger" lay-event="delete">删除</button>
            <button class="layui-btn layui-btn-sm" lay-event="update">编辑</button>
        </div>
    </script>
    <!--修改表单-->
    <form class="layui-form" id="updateForm" lay-filter="updateFormFilter" style="display: none;padding: 15px 15px 5px 5px"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
        <div class="layui-form-item">
            <label class="layui-form-label">轮播图名称</label>
            <div class="layui-input-block">
                <input type="hidden" name="bannerId"/>
                <input type="hidden" name="bannerImageUrl"/>
                <input type="text" name="bannerOldName" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">轮播图片</label>
            <div class="layui-input-block">
                <button type="button" class="layui-btn" id="updateImg">
                    <i class="layui-icon">&#xe67c;</i>上传图片
                </button>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">轮播图状态</label>
            <div class="layui-input-block">
                <input type="radio" name="bannerState" value="0" title="已过期">
                <input type="radio" name="bannerState" value="1" title="禁用">
                <input type="radio" name="bannerState" value="2" title="启用">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">轮播图描述</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入内容" name="bannerDescription" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="updateFilter">修改</button>
            </div>
        </div>
    </form>
    <!--layui代码-->
    <script>
        layui.use(['table','form','upload','laydate','layer'],function () {
            var table = layui.table;
            var form = layui.form;
            var upload = layui.upload;
            var laydate = layui.laydate;
            var layer = layui.layer;
            var $ = layui.$;
            //1、渲染表格
            table.render({
                elem:'#myTable',
                url:'${pageContext.request.contextPath}/cmfzBanner/show',
                page:true,
                limit:3,
                limits:[3,4,5],
                cols:[[
                    {field:'bannerId',title:'编号',sort:true,width:120},
                    {field:'bannerOldName',title:'轮播图名称',sort:true,width:120},
                    {field:'bannerImageUrl',title:'轮播图片',templet:'#showImg'},
                    // {field:'bannerImageUrl',title:'轮播图片',sort:true,width:120},
                    {field:'bannerState',title:'轮播图状态',templet:function (b) {
                            if(b.bannerState==0){
                                return '已过期'
                            }else if(b.bannerState==1){
                                return '禁用'
                            }else{
                                return '启用'
                            }
                        }},
                    {field:'bannerDate',title:'上传日期',sort:true,width:120},
                    {field:'bannerDescription',title:'描述',sort:true,width:120},
                    {title:'操作',toolbar:'#bar'}
                ]]
            })
            //2.添加操作
            $('#add').click(function () {
                layer.open({
                    type:1,
                    title:'添加页面',
                    area:'900px',
                    content:$('#addForm')
                })
            })
            //2.1、加载日期组件
            laydate.render({
                elem: '#addDate'
            })
            //2.2.加载文件上传组件
            upload.render({
                elem:'#addImg',
                url: '${pageContext.request.contextPath}/cmfzBanner/upload',
                done:function (result) {
                    if(result.flag){
                        layer.msg('上传成功');
                        form.val('addFormFilter',{//上传成功 回显上传后的文件名到表单隐藏起来
                            bannerImageUrl:result.fileName
                        })
                    }else {
                        layer.msg('上传失败')
                    }
                }
            })
            //2.3、提交表单
            form.on('submit(addFilter)',function () {
                $.ajax({
                    url:'${pageContext.request.contextPath}/cmfzBanner/add',
                    data:$('#addForm').serialize(),
                    success:function (result) {
                        if(result.flag){
                            layer.msg('添加成功');
                            layer.closeAll();
                            $('#addForm')[0].reset();
                            table.reload('myTable');
                        }else {
                            layer.msg('添加失败');
                        }
                    }
                })
                return false;
            })
            //3.监听toolbar工具栏组件  执行删除和修改操作
            table.on('tool(tableFilter)',function (obj) {
                console.log(obj.data.bannerId)
                if(obj.event=='delete'){
                    //执行删除操作
                    layer.confirm('确认要删除该项吗？',function (index) {
                        $.ajax({
                            url:'${pageContext.request.contextPath}/cmfzBanner/remove',
                            data:'bannerId='+obj.data.bannerId,
                            success:function (result) {
                                if(result.flag){
                                    layer.msg('删除成功');
                                    table.reload('myTable');
                                }else {
                                    layer.msg('删除失败');
                                }
                            }
                        })
                        layer.close(index);
                    })
                }else if(obj.event=='update'){
                    //执行修改操作
                    layer.open({
                        type:1,
                        title:'修改页面',
                        area:'900px',
                        content:$('#updateForm')
                    })
                    //回显
                    $.ajax({
                        url:'${pageContext.request.contextPath}/cmfzBanner/getOne',
                        data:'bannerId='+obj.data.bannerId,
                        success:function (result) {
                            form.val('updateFormFilter',{
                                bannerId:result.bannerId,
                                bannerOldName:result.bannerOldName,
                                bannerState:result.bannerState,
                                bannerDescription:result.bannerDescription,
                                bannerImageUrl:result.bannerImageUrl
                            })
                        }
                    })
                    upload.render({
                        elem:'#updateImg',
                        url: '${pageContext.request.contextPath}/cmfzBanner/upload',
                        done:function (result) {
                            if(result.flag){
                                layer.msg('上传成功');
                                form.val('updateFormFilter',{//上传成功 回显上传后的文件名到表单隐藏起来
                                    bannerImageUrl:result.fileName
                                })
                            }else {
                                layer.msg('上传失败')
                            }
                        }
                    })
                    //提交更新表单
                    form.on('submit(updateFilter)',function () {
                        $.ajax({
                            url:'${pageContext.request.contextPath}/cmfzBanner/update',
                            data:$('#updateForm').serialize(),
                            success:function (result) {
                                if(result.flag){
                                    layer.msg('修改成功');
                                    layer.closeAll();
                                    table.reload('myTable');
                                }else {
                                    layer.msg('修改失败');
                                }
                            }
                        })
                        return false;
                    })
                }
            })
        })
    </script>
</body>
</html>
